local Lua__shizhi = fk.CreateSkill {
  name = "Lua__shizhi",
}

Fk:loadTranslationTable {
  ["Lua__shizhi"] = "时至",
  ["@Lua__shizhi"] = "时至",
  ["#Lua__shizhi-discard"] = "时至:将手牌调整至%arg张",
  ["#Lua__shizhiActive"] = "时至",
  ["#Lua__shizhi"] = "时至：你可以记录所有角色的体力值与手牌数",
  [":Lua__shizhi"] = "出牌阶段限一次或你受到伤害后，你可以记录所有角色的体力值与手牌数。一名角色死亡后，你可以令所有存活角色将体力值与手牌数调整至上次记录的数值。",
  ["#shizhi-invoke"] = "时至：你可以令所有存活角色将体力值与手牌数调整至上次记录的数值",
  ["@shizhi_record"] = "时至",

}

local M = require "packages.internetMEME.record.INTTE"

Lua__shizhi:addEffect("active", {
  anim_type = "control",
  prompt = "#Lua__shizhi",
  can_use = function(self, player)
    return player:usedSkillTimes(Lua__shizhi.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    for _, p in ipairs(player.room.alive_players) do
      player.room:setPlayerMark(p, "@Lua__shizhi",
        { p.hp, p:getHandcardNum() })
    end
  end,
})

Lua__shizhi:addEffect(fk.Damaged, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill("Lua__shizhi") then return false end
    return target == player
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return player.room:askToSkillInvoke(player, { skill_name = Lua__shizhi.name, })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(player.room.alive_players) do
      player.room:setPlayerMark(p, "@Lua__shizhi",
        { p.hp, p:getHandcardNum() })
    end
  end,
})
Lua__shizhi:addEffect(fk.Death, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill("Lua__shizhi") then return false end
    return target ~= player
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if player:getTableMark("@Lua__shizhi") == 0 then return false end
    return player.room:askToSkillInvoke(player, { skill_name = Lua__shizhi.name, })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(player.room.alive_players) do
      if #p:getTableMark("@Lua__shizhi") == 0 then return end
      local p_hp = p:getTableMark("@Lua__shizhi")[1]
      local p_handnum = p:getTableMark("@Lua__shizhi")[2]
      if p.hp > p_hp then
        room:changeHp(p, -math.abs(p.hp - p_hp))
      else
        room:changeHp(p, math.abs(p.hp - p_hp))
      end
      M.ChangeHandNum(p, p_handnum,
        "#Lua__shizhi-discard:::" .. p_handnum, self)
    end
  end,
})

return Lua__shizhi
